﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL
{
    public class DiemMonBLL
    {
        static int id = 0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public IQueryable SelectDes(string mamon)
        {
            return DB.DiemMons.Join(
                DB.MonHocs,
                a => a.MaMonHoc,
                b => b.MaMonHoc,
                (a, b) => new
                {
                    a.MaMonHoc,
                    b.TenMon,
                    a.MaTP,
                    a.SoLuong
                }
                ).Join(
                DB.DiemThanhPhans,
                a => a.MaTP,
                b => b.MaTP,
                (a, b) => new
                {
                    a.MaMonHoc,
                    a.TenMon,
                    b.TenThanhPhan,
                    a.SoLuong
                }
                ).Where(s=>s.MaMonHoc==mamon).OrderByDescending(s => s.MaMonHoc).AsQueryable();
        }

        public string Delete(string mamon,string matp)
        {
            try
            {
                DiemMon dm = DB.DiemMons.Where(s => s.MaMonHoc == mamon && s.MaTP == matp).FirstOrDefault();
                DB.DiemMons.DeleteOnSubmit(dm);
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string[] GetTenTo()
        {
            string[] data = new string[1000];
            data = (from t in DB.Tos
                    select t.TenTo).ToArray();
            return data;
        }

        public string[] GetTenMon()
        {
            string[] data = new string[1000];
            data = (from t in DB.MonHocs
                    select t.TenMon).ToArray();
            return data;
        }

        public string[] GetTenDiem()
        {
            string[] data = new string[1000];
            data = (from t in DB.DiemThanhPhans
                    select t.TenThanhPhan).ToArray();
            return data;
        }

        public string GetMaTo(string tento)
        {
            string mato = DB.Tos.Where(tk => tk.TenTo == tento).Select(th => th.MaTo).FirstOrDefault().ToString();
            return mato;
        }

        public string GetMaMon(string mato, string tenmon)
        {
            string mamon = DB.MonHocs.Where(tk => tk.TenMon == tenmon).Select(th => th.MaMonHoc).FirstOrDefault().ToString();
            return mamon;
        }

        public string GetMaTP(string tentp)
        {
            string matp = DB.DiemThanhPhans.Where(tk => tk.TenThanhPhan == tentp).Select(th => th.MaTP).FirstOrDefault().ToString();
            return matp;
        }

        public string Insert(DiemMonDTO diemmondto)
        {
            try
            {
                DiemMon diemmon = new DiemMon();
                diemmon.MaMonHoc = diemmondto.Mamonhoc;
                diemmon.MaTP = diemmondto.Matp;
                diemmon.SoLuong = diemmondto.Soluong;
                DB.DiemMons.InsertOnSubmit(diemmon);
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public List<MonDTO> getMonTheoTo(string mato)
        {
            //lay het du lieu tu bang khoi
            List<MonHoc> list = DB.MonHocs.Where(s=>s.MaTo==mato).ToList<MonHoc>();
            //tao moi 1 doi tuong KhoiDTO de chua du lieu cua bang khoi
            List<MonDTO> listdto = new List<MonDTO>();
            foreach (MonHoc lop in list)
            {
                MonDTO k = new MonDTO();
                k.MaMonHoc = lop.MaMonHoc;
                k.TenMonHoc= lop.TenMon;
                listdto.Add(k);
            }
            return listdto;
        }
        public List<DiemThanhPhanDTO> getDiemThanhPhan()
        {
            //lay het du lieu tu bang khoi
            List<DiemThanhPhan> list = DB.DiemThanhPhans.ToList<DiemThanhPhan>();
            //tao moi 1 doi tuong KhoiDTO de chua du lieu cua bang khoi
            List<DiemThanhPhanDTO> listdto = new List<DiemThanhPhanDTO>();
            foreach (DiemThanhPhan lop in list)
            {
                DiemThanhPhanDTO k = new DiemThanhPhanDTO();
                k.MaTP = lop.MaTP;
                k.TenThanhPhan = lop.TenThanhPhan;
                listdto.Add(k);
            }
            return listdto;
        }
    }
}
